package com.huaxin.hxcloud.activity;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.huaxin.common.Entity.HxInHousePollingBean;
import com.huaxin.common.utils.ToastUtil;
import com.huaxin.hxcloud.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.huaxin.hxcloud.activity.WorkTaskListActivity.WORK_TASK_STATE_END;
import static com.huaxin.hxcloud.activity.WorkTaskListActivity.WORK_TASK_STATE_NOT_START;
import static com.huaxin.hxcloud.activity.WorkTaskListActivity.WORK_TASK_STATE_START;

/**
 * Created by songxf on 2017/9/4.
 * 厂内巡检Adapter
 */

public class HxInHousePollingAdpater extends RecyclerView.Adapter<HxInHousePollingAdpater.ViewHolder> {


    private       List<HxInHousePollingBean.DataBean> mDataBeanList;
    private       Context                             mContext;
    private final LayoutInflater                      mInflater;
    private       int                                 mState;

    public HxInHousePollingAdpater(Context context, List<HxInHousePollingBean.DataBean> dataBeanList) {
        this.mContext = context;
        this.mDataBeanList = dataBeanList;
        mInflater = LayoutInflater.from(context);
    }

    /**
     * 判断是否有开启任务
     *
     * @param state
     */
    public void setState(int state) {
        this.mState = state;
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View rootView = mInflater.inflate(R.layout.item_worktask1, parent, false);
        return new ViewHolder(rootView);
    }

    /**
     * 修改巡检状态
     *
     * @param position
     * @param finish
     */
    public void setFinishState(int position, int finish) {
        HxInHousePollingBean.DataBean dataBean = mDataBeanList.get(position);
        dataBean.setIsFinish(finish);
        notifyDataSetChanged();
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        final HxInHousePollingBean.DataBean dataBean = mDataBeanList.get(position);
        //构筑物名称
        holder.mTvBuilding.setText(("构筑物：" + dataBean.getStructureName()));
        //描述
        holder.mTvDes.setText(("描述：" + dataBean.getDescription()));
        //厂站名称
        holder.mTitle.setText((dataBean.getStationName() + ""));
        String typeState = "";
        // state 1:已完成，0:未完成
        switch (dataBean.getIsFinish()) {
            case 0:
                typeState = "未完成";
                break;
            case 1:
                typeState = "已完成";
                break;
        }
        holder.mTvState.setText(typeState);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mState == WORK_TASK_STATE_NOT_START) {
                    ToastUtil.showToast("请先开启任务");
                } else if (mState == WORK_TASK_STATE_START||mState==WORK_TASK_STATE_END) {
                    HxFacilityTaskDetailActivity.start((WorkTaskListActivity) mContext, dataBean, holder.getLayoutPosition());
                }
            }


        });
    }


    @Override
    public int getItemCount() {
        return mDataBeanList == null ? 0 : mDataBeanList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_state)
        TextView mTvState;
        @BindView(R.id.tv_building)
        TextView mTvBuilding;
        @BindView(R.id.tv_des)
        TextView mTvDes;
        @BindView(R.id.title)
        TextView mTitle;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
